package com.ymatou.iapi.prod.testcase;

import java.util.ArrayList;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONArray;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.prod.parameter.GetProductDescriptionInfoBean;
import com.ymatou.iapi.prod.service.GetProductDescriptionInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.ProductDescriptionModelWapper;
import com.ymttest.utils.EnvSetup;

public class Ts_GetProductDescriptionInfo {
	
	private static GetProductDescriptionInfoBean getProductDescriptionInfoBean;
	private static GetProductDescriptionInfoCall getProductDescriptionInfoCall;
	
	private static int sellerId = Integer.parseInt(EnvSetup.getData("zhangyi_seller002_id"));
	private static String sellerName = EnvSetup.getData("zhangyi_seller002_name");

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商品描述信息");
	}

	@Before
	public void caseUp() throws Exception {
		getProductDescriptionInfoBean = new GetProductDescriptionInfoBean();
		getProductDescriptionInfoCall = new GetProductDescriptionInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getProductDescriptionInfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_GetProductDescriptionInfo_001() {
		 Logger.start(true,"商品描述信息接口与mongodb数据验证");
		 try {
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 //String productId = "ba865a56-0eda-4f4c-8183-050aaa081f06";
			 getProductDescriptionInfoBean.setProductId(productId);
			 
			 getProductDescriptionInfoCall.setData(getProductDescriptionInfoBean);
			 getProductDescriptionInfoCall.callService();
			 
			 ProductDescriptionModelWapper wapper = new ProductDescriptionModelWapper();
			 Map<String,Object> map = wapper.getProductId(productId);
			 
			 String description = map.get("Description").toString();
			 
			 Logger.verifyEquals(this.getStringByTwoSide(description, "ProductDescText\">", "</div>"), getProductDescriptionInfoCall.getResult().get("text"), 
					 "验证text是否与mongodb一致");
			 
			 JSONArray pics = new JSONArray(getProductDescriptionInfoCall.getResult().get("pics").toString());
			 ArrayList<String> picList = this.getImageUrl(description);
			 
			 for (int i = 0; i < pics.length() - 1; i++) {
				 Logger.verifyEquals(picList.get(i), pics.getString(i), "验证pics是否与mongodb一致");
			 }
			 
			 Logger.verifyEquals(pics.get(pics.length() - 1), "http://staticontent.ymatou.com/app/desc/pricedesc.png", "验证默认价格图片地址是否正确");
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@SuppressWarnings("null")
	public String getStringByTwoSide(String origin,String leftSide, String rightSide) {
		
		if (null != origin|| origin.equalsIgnoreCase("")) {
			return origin.split(leftSide)[1].split(rightSide)[0];
		}
		else
			return "origin不正确";
	}
	
	public ArrayList<String> getImageUrl(String htmlCode) {
		ArrayList<String> list = new ArrayList<String>();
		
		Matcher m = Pattern.compile("<img\\b[^>]*\\bsrc\\b\\s*=\\s*('|\")?([^'\"\n\r\f>]+(\\.jpg|\\.bmp|\\.eps|\\.gif|\\.mif|\\.miff|\\.png|\\.tif|\\.tiff|\\.svg|\\.wmf|\\.jpe|\\.jpeg|\\.dib|\\.ico|\\.tga|\\.cut|\\.pic)\\b)[^>]*>", Pattern.CASE_INSENSITIVE)
			      .matcher(htmlCode);
		 String quote = null;
	     String src = null;
	     while (m.find()) {
	         quote = m.group(1);
	         src = (quote == null || quote.trim().length() == 0) ? m.group(2).split("//s+")[0] : m.group(2);
	         list.add(src);
	     }
	     
	     return list;
	}
}
